package com.bolang.onlycode.demo.MyThread;

import java.util.List;

public class Consumer implements Runnable{
    private List<Water> waterList;

    public Consumer(List<Water> waterList) {
        this.waterList = waterList;
    }

    @Override
    public void run() {
        while (true) {
            /**
             * 获得锁
             */
            synchronized (waterList) {
                /**
                 * 证明没有水可以消费
                 */
                while (waterList.isEmpty()) {
                    System.out.println("消费者被访问了");
                    try {
                        /**
                         * 释放锁
                         */
                        waterList.wait();
                    } catch (InterruptedException e) {
                    }
                }
                /**
                 * 每次我都移动第一个元素
                 */
                waterList.remove(0);
                System.err.println(Thread.currentThread().getName()+"消费了1L水，现在水桶有：" + waterList.size() + "L水");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                /**
                 * 通知生产者生产
                 */
                waterList.notifyAll();
            }
        }

    }
}
